On this page of our tutorial we show you how to introduce new classes (= types). In fact, this is a live specification of a grammar — immediately equipped with a parser. The parser recognizes whether an input string belongs to one of the classes. You should notice that in Speagram specifying a grammar and writing a parser for this grammar is one and the same piece of code.
It's a very simple parser for English syntax. It can recognize sentences like:
This boy wants this girl. People have cats. Tom needs some money. I read a book. He reads these books. Cats need dogs. These dogs want those cats.
With this simple example we want to show you how close Speagram's spirit is to natural language. You can write a normal English sentence and Speagram will recognize it. It would be easy to expand the program to correct sentences with bad inflection like He have a book. In fact, this little program would recognize this sentence as a sentence with bad inflection.
For those programmers who are well acquainted with functional programming this example may be so clear that they will understand how it's done simply by looking at the code. However, even if you are not comfortable with functional programming, you should still be able to follow what's going on because we provide explanatory comments and transcripts from the command line console alongside with the code.
Let's begin to go through the code. We are first going to load the basic library and define a few nouns.
Load state library:/basic. New class singular noun. Book is a singular noun. Girl is a singular noun. Boy is a singular noun. Cat is a singular noun. Dog is a singular noun.
The first line (starting with New class) introduces a new class - or type. The name of the class is singular noun. Indeed, Speagram's parser can recognize names composed of two or more words separated by spaces. This is unusual for a programming language but this is quite usual in everyday human language. Since the most important goal of the Speagram Project is to bridge the gap between the way language is used naturally by people and the way computers are programmed, we thought it only natural to allow such names.
In fact, we recommend to take full advantage of our powerful parser by choosing full names, however long, which call the thing exactly by its name. In other words, if something has a name in natural language, you can use that name in Speagram.
The next lines introduce five elements of the class singular noun
.
From now on, the words book, girl, boy, cat,
and dog
will be seen by the Speagram parser
as elements of the singular noun
class — that is as singular nouns simply.
Notice that we use double apostrophes ('') around each new word in edit mode. This is similar to the habit of underlining the term which is being defined when writing a definition and it is coherent with wiki conventions. As you can see below it is sometimes possible to use the apostrophes around two or three words instead of using them around each new one and our web interface understands this emphasis.
You can now try for yourself and see how Speagram responds to words at this point.
Just edit this field and click "Run" above to see Speagram response.
girl. dog.
Quite similarly, we define the classes
plural noun, uncountable noun, singular determiner, plural determiner, and uncountable determiner.
Note that instead of is a
you can use is an
or new element as
.
New class plural noun. Books is a plural noun. Girls is a plural noun. Boys is a plural noun. Cats is a plural noun. Dogs is a plural noun. New class uncountable noun. Money is an uncountable noun. Time is an uncountable noun. New class singular determiner. New element a as singular determiner. The is a singular determiner. This is a singular determiner. That is a singular determiner. New class plural determiner. Empty plural delimiter is a plural determiner. Some is a plural determiner. The is a plural determiner. These is a plural determiner. Those is a plural determiner. New class uncountable determiner. The is an uncountable determiner. This is an uncountable determiner. That is an uncountable determiner. Some is an uncountable determiner.
Again, you can try for yourself how Speagram parses these elements.
these. a. money.
Now let's take a look at how we define the class singular noun phrase
.
New class singular noun phrase. New element singular determiner singular noun as singular noun phrase.
We teach the parser that a singular determiner
immediately before a singular noun
should be recognized
as a singular noun phrase
. Now, that girl, this book, a cat
are elements of the class singular noun phrase
.
Try yourself to run it:
this book. a cat.
Let's move on to the plural noun phrase.
New class plural noun phrase. Plural noun is a plural noun phrase. See last element as cast. Plural determiner plural noun is a plural noun phrase.
The first element declaration states that a plural noun is actually a plural noun phrase.
The line See last element as cast.
serves to disambiguate the situation.
You see, now dog
could be parsed as an element of plural noun
or as an element of plural noun phrase
.
This disambiguating line sets the preference in favor of the original class (which is plural noun
).
The "type cast" will be performed by the parser only when it is necessary.
The second element declaration is similar to that in the previous class.
Try yourself to run it:
dogs. these dogs.
Let's go on with the code.
New class uncountable noun phrase. New element uncountable determiner uncountable noun as uncountable noun phrase. New class complement. Singular noun phrase is a complement. See last element as cast. Plural noun phrase is a complement. See last element as cast. Uncountable noun phrase is a complement. See last element as cast.
Now there are three kinds of elements in the class complement, for example a dog, these cats, some money
.
Notice that at this point there could be an ambiguity, for example: a boy
is an element of two classes:
singular noun phrase
and complement
. However, thanks to the lines
See last element as cast
. this disambiguity is solved in favor of the original classes.
Try yourself to run it:
a boy. these cats. some money.
At this point we will only mention (without going into details) that Speagram has a system of priorities that serves to disambiguate expressions in situations where one meaning is clearly used more often or in some other way dominates the other meaning.
Since English has a residual inflectional system that shows itself in the form of the -s ending in third person singular we will define two classes for the subject of a sentence. One will be for subjects that require a verb with no ending and the other will require a verb with the -s ending.
New class subject. Plural noun phrase is a subject. See last element as cast. I is a subject. You is a subject. We is a subject. They is a subject. People is a subject. New class s_subject. Singular noun phrase is a s_subject. See last element as cast. He is a s_subject. She is a s_subject. Tom is a s_subject. Kate is a s_subject.
Try yourself to run it:
He. People. These cats. a girl.
The following classes present no new features.
New class verb. Have is a verb. Want is a verb. Need is a verb. See is a verb. Read is a verb. New class s_verb. Has is a s_verb. Wants is a s_verb. Needs is a s_verb. Sees is a s_verb. Reads is a s_verb. New class verb phrase. Verb complement is a verb phrase. New class s_verb phrase. New element s_verb complement as s_verb phrase.
Try yourself to run it:
has a book. want some time.
Finally, we will teach our parser to recognize correct sentences and sentences with bad inflection.
New class sentence. New element subject verb phrase as sentence. New element s_subject s_verb phrase as sentence. New class sentence with bad inflection. New element subject s_verb phrase as sentence with bad inflection. New element s_subject verb phrase as sentence with bad inflection. Close context.
Let's try more examples:
This boy wants a dog. People have cats. Tom need a girl. Kate has a boy. I want some money. We sees these cats. I see these book.
Notice that the last incorrect sentence was not recognized by the parser as anything in particular. It just doesn't have the syntax to perceive them for anything else than non-parsable.
Summary
We have learned how to introduce new classes in Speagram. A class can contain specifically named elements like money
or be composed of a particular juxtaposition of elements of other classes. We will see an example of a more complicated class in the symbolic differentiation example.
We have seen how close to natural language a Speagram program can be, and most importantly how easy it is for the Speagram parser to perceive English sentences. Naturally, this little example can be extended to cover many more grammatical structures and to recognize many more words. It's up to you to experiment with Speagram's powerful possibilities. We believe that we have started to convince you that Speagram is something to be reckoned with when it comes to bringing the computer and the human together.